package com.zhagnxy.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtil {

    private static SqlSessionFactory ssf = null;

    /**
     * 初始化SqlSessionFactory类型变量ssf(xml资源文件inputStream方式)
     *
     * @return
     */
    public static SqlSessionFactory getSqlSessionFactory() {
        if (ssf == null) {
            try {
                InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
                getSqlSessionFacotry0(null, is);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ssf;
    }

    /**
     * 初始化SqlSessionFactory类型变量ssf(Configuration类对象方式)
     *
     * @param configuration
     * @return
     */
    public static SqlSessionFactory getSqlSessionFactory(Configuration configuration) {
        if (ssf == null) {
            getSqlSessionFacotry0(configuration, null);
        }
        return ssf;
    }

    /**
     * 通过configuration或inputStream方式初始化SqlSessionFactory类型变量ssf
     *
     * @param configuration Configuration类对象方式
     * @param inputStream   xml资源文件inputStream方式
     */
    private synchronized static void getSqlSessionFacotry0(Configuration configuration, InputStream inputStream) {
        if (ssf == null) {
            if (configuration != null) {
                ssf = new SqlSessionFactoryBuilder().build(configuration);
            } else if (inputStream != null) {
                ssf = new SqlSessionFactoryBuilder().build(inputStream);
            }
        }
    }

    /**
     * 创建Configuration对象，使用Configuration对象实例来配置myBatis上下文，对于用使用myBatis的xml配置文件
     * @return
     */
/*	public static Configuration bulidConfiguraction() {

		DataSource dataSource = C3P0Pool.getComboPooledDataSource();
		TransactionFactory transactionFactory = new JdbcTransactionFactory();
		Environment envi = new Environment("development_ABC", transactionFactory, dataSource);

		Configuration configuration = new Configuration(envi);
		configuration.setLazyLoadingEnabled(true);
		configuration.addMapper(MkTestTableMapperI.class);

		return configuration;
	}*/

    /**
     * 获取一个非自动提交的SqlSession
     *
     * @return
     */
    public static SqlSession getSqlSession() {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
        return sqlSession;
    }

    /**
     * 获取SqlSession
     *
     * @param isAutoCommit true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务
     *                     false 表示创建的SqlSession对象在执行完SQL之后不会自动提交事务,这时就需要我们手动调用sqlSession.commit()提交事务
     * @return SqlSession
     */
    public static SqlSession getSqlSession(boolean isAutoCommit) {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession(isAutoCommit);
        return sqlSession;
    }

    public static SqlSession getSqlSession(Configuration configuration, boolean isAutoCommit) {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory(configuration).openSession(isAutoCommit);
        return sqlSession;
    }
}